<?php
/**
 * @see Alinks_Files
 */
require_once('Alinks/Files.php');

/**
 * Factory class for creating Alinks_Cacher_Interface instances
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Factory.php 387 2008-07-03 05:35:19Z sean $
 * @package Alinks_Cacher
 */
class Alinks_Cacher_Factory
{
	/**
	 * Creates a new instace of Alinks_Cacher_Interface
	 *
	 * Given the type of cacher to create, and the cacher options, this
	 * method returns a new cacher of that type.
	 *
	 * @param string $type The type of cacher
	 * @param array $options The cacher options
	 * @return Alinks_Cacher_Interface
	 */
	public static function factory($type, array $options)
	{
		$type = ucwords(strtolower($type));
		$class = 'Alinks_Cacher_' . $type;
		if (!class_exists($class)) {
			$file = "Alinks/Cacher/$type.php";
			if (!Alinks_Files::exists($file)) {
				/**
				 * @see Alinks_Cacher_Exception
				 */
				require_once('Alinks/Cacher/Exception.php');
				throw new Alinks_Cacher_Exception("No cacher is installed for the type $type. Searched for $file");
			}
			
			require_once($file);
			if (!class_exists($class)) {
				/**
				 * @see Alinks_Cacher_Exception
				 */
				require_once('Alinks/Cacher/Exception.php');
				throw new Alinks_Cacher_Exception("Found $file, but not class $class");
			}
		}
		
		return new $class($options);
	}
}